package com.goodflower.goodflower.chang_user;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.goodflower.goodflower.FlowerActivity;
import com.goodflower.goodflower.MainActivity;
import com.goodflower.goodflower.R;
import com.goodflower.goodflower.address.Address;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.util.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Change_user_password extends AppCompatActivity {
    private Button btn_next;
    private EditText password_old;
    private ProgressDialog login_pg;//登录时后的请求
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_user_password);
        //找到控件
        btn_next = (Button)findViewById(R.id.btn_change_user_password_one_next);
        password_old = (EditText)findViewById(R.id.change_user_password_old);
        //按钮点击事件
        login_pg = new ProgressDialog(this);
        login_pg.setMessage("正在验证,请稍后");
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String old_password = password_old.getText().toString();
                if(old_password == null || old_password.isEmpty()){
                    Toast.makeText(Change_user_password.this,"请输入您的原密码",Toast.LENGTH_LONG).show();
                }else {
                    login_pg.show();
                    HttpUtil.sendOkHttpRequest(Address.ADDRESS_LOGIN+"name="+Info.current_user_name+"&password="+password_old.getText().toString(), new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Toast.makeText(Change_user_password.this,"网络请求失败,请检查网络设置",Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            /********测试专用*********/
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            /****************/
                            login_pg.dismiss();
                            String httpresponse = response.body().string();
                            if(httpresponse.equals("ok")){
                                Log.d("login","登陆成功");
                                Intent intent = new Intent(Change_user_password.this,Change_user_password_ok.class);
                                startActivity(intent);

                            }
                            else {
                                Looper.prepare();

                                AlertDialog.Builder login_false = new AlertDialog.Builder(Change_user_password.this);
                                login_false.setTitle("验证失败");
                                login_false.setMessage("请检查您的密码");
                                login_false.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                    }
                                });
                                login_false.show();
                                Looper.loop();
                            }
                        }
                    });
                }
            }
        });
    }
}
